package sketchpad.business.datacalc.calchelper.impl.featurenode;

import sketchpad.gui.drawinggraph.*;
import sketchpad.gui.drawinggraph.node.Node;

import java.util.ArrayList;
import java.util.List;

/*
* 这个类是为了避免重绘选择框时（需要选择合适大小截图识别）选择特征点的函数大量代码造成阅读困难
* */
public final class AddFeatureNodeUtil {

    private AddFeatureNodeUtil(){}//静态类，不允许创建浪费性能

    private static List<Node>  featureNode = new ArrayList<>();

    public static void addFeatureNodeWhenRectangle(DrawItem item){
        Rectangle rectangle = (Rectangle)item;
        double x1 = rectangle.getLeftUpNode().x;
        double y1 = rectangle.getLeftUpNode().y;
        double x2 = rectangle.getRightDownNode().x;
        double y2 = rectangle.getRightDownNode().y;
        double centX = (x1+x2)/2;
        double centY = (y1+y2)/2;
        featureNode.add(new Node(x1,y1));
        featureNode.add(new Node(x1,centY));
        featureNode.add(new Node(x1,y2));
        featureNode.add(new Node(centX,y1));
        featureNode.add(new Node(centX,y2));
        featureNode.add(new Node(x2,y1));
        featureNode.add(new Node(x2,centY));
        featureNode.add(new Node(x2,y2));
    }

    public static void addFeatureNodeWhenTriangle(DrawItem item){
        Triangle triangle = (Triangle)item;
        double x1 = triangle.getLeftUpNode().x;
        double y1 = triangle.getLeftUpNode().y;
        double x2 = triangle.getRightDownNode().x;
        double y2 = triangle.getRightDownNode().y;
        double x3 = x1+x1-x2;
        double centY = (y1+y2)/2;
        featureNode.add(new Node(x1,y1));
        featureNode.add(new Node((x1+x2)/2,centY));
        featureNode.add(new Node((x3+x1)/2,centY));
        featureNode.add(new Node(x2,y2));
        featureNode.add(new Node(x1,y2));
        featureNode.add(new Node(x3,y2));
    }

    public static void addFeatureNodeWhenCircle(DrawItem item){
        Circle circle = (Circle)item;
        Node n1 = circle.getLeftUpNode();
        Node n2 = circle.getRightDownNode();
        double a = Math.abs(n1.x-n2.x)/2;
        double b = Math.abs(n1.y-n2.y)/2;
        double centX = (n1.x+n2.x)/2;
        double centY = (n1.y + n2.y)/2;
        double step = (n2.x-n1.x)/4;
        double pos = n1.x;
        int featureNodeNum = 5;
        while (featureNodeNum-->0){
            double y2 = (a*a*b*b-b*b*(pos-centX)*(pos-centX));//(y-centY)^2
            if (y2==0){
                featureNode.add(new Node(pos,centY));
            }else {
                featureNode.add(new Node(pos,centY+Math.sqrt(y2)));
                featureNode.add(new Node(pos,centY-Math.sqrt(y2)));
            }
            pos+=step;
        }
    }

    public static void addFeatureNodeWhenRandom(DrawItem item){
        Random random = (Random)item;
        List<Node> randomList = random.getNodeList();
        for (int j = 0 ; j < randomList.size(); j+=5){
            featureNode.add(randomList.get(j));
        }
    }

    public static List<Node> getFeatureNode() {
        return featureNode;
    }
}
